
'use strict';

const express=require('express');
const path=require('path');
const xtpl=require("xtpl");
const moment=require("moment");
const md5=require("md5");
const db=require(path.join(__dirname,"../configs/database.js"));
const base=require(path.join(__dirname,"./baseController.js"));
module.exports.adminAdd=function(req,res){
   xtpl.renderFile(
     path.join(__dirname,"../views/admin/admin_add.html"),
     {},
     function(err,content){
            res.setHeader("Content-Type","text/html;charset=utf-8");
            res.end(content);
     }

   	);
};

module.exports.adminAddData=function(req,res){
 var where =`admin_name='${req.body.admin_name}'`;
 db.table('admin').where(where).find().then(function(data){

  if(JSON.stringify(data)!="{}"){

      res.setHeader("Content-Type","text/html;charset=utf-8");
      res.end("<script>alert('该管理员已经存在');location.href='/admin/admin_add'</script>");
      return false;

  }else{
     
       var data={
          "admin_name":req.body.admin_name,
          "admin_pwd":md5(req.body.admin_pwd),
          "admin_time":(Date.parse(moment(req.body.admin_time))/1000)

       };

      
       // 插入数据
    
   db.table('admin').add(data).then(function(insertId){
        res.redirect("admin/admin_list");
   }).catch(function(err){
   	  if(err){
   	  	return console.log(err);
   	  }
   });

  }

 });


};


// 管理员列表页

module.exports.adminLdit=function(req,res){
  const page=parseInt(req.query.page||'1');
  const limit=parseInt(req.query.limit||'12');
  const start=(page-1)*limit;

  db.table("admin").count("*").then(function(count){
       
       const totalPage=count%limit==0?count/limit:parseInt(count/limit)+1;
       const totalPageArray=[];
       for(var i=1;i<=totalPage;i++){
          totalPageArray.push(i);

       }

       db.table("admin").order("admin_id DESC").limit(start,limit).select().then(function(data){


            xtpl.renderFile(
                 
                 path.join(__dirname,"../views/admin/admin_list.html"),
                 {
                 	array:data,
	                totalPage:totalPage,
	                totalPageArray:totalPageArray,
	                page:page
                 },
                 function(err,content){

                   if(err){
                      return console.log(err);
                   }
                   res.setHeader("Content-Type","text/html;charset=utf-8");
                   res.end(content);

                 }

            	);
       });
  });
};

module.exports.adminEdit=function(req,res){

 let amdin_id=req.params.admin_id||0;

 let where=`admin_id=${amdin_id}`;

 db.table('admin').where(where).find().then(function(data){
   if(JSON.stringify(data)=="{}"){
         base.showMsg(res,'管理员不存在','/admin/admin_list');
         return false;
   }else{

  
      data.admin_time=moment(data.admin_time*1000).format("YYYY-MM-DD");
     xtpl.renderFile(
        path.join(__dirname,"../views/admin/admin_edit.html"),
        {

        	admin:data
        },
        function(err,content){
          res.setHeader("Content-Type","text/html;charset=utf-8");
          res.end(content);
        }
      	);
   }
 });

};


// 修改提交的信息

module.exports.adminEditAdd=function(req,res){
 
 let admin_id=req.params.admin_id||0;
 let where=`admin_id=${admin_id}`;

 db.table("admin").where(where).find().then(function(data){

   if(JSON.stringify(data)=="{}"){
       base.showMsg(res,'管理员不存在','/admin/admin_list');
       return false;
   }else{

     var data={
          "admin_name":req.body.admin_name,
          "admin_pwd":md5(req.body.admin_pwd),
          "admin_time":(Date.parse(moment(req.body.admin_time))/1000)

       };
          
       db.table("admin").where(`admin_id=${admin_id}`).update(data).then(function(affectRows){
         
         res.redirect('/admin/admin_list');

         return false;
       }).catch(function(err){

            if(err){
            	base.showMsg(res,"更新失败");
            	return false;
            }

       });


   }



 });

};

module.exports.adminDelete=function(req,res){
  let admin_id=req.params.admin_id;
  let where=`admin_id=${admin_id}`;
  db.table("admin").where(where).delete().then(function(adminRows){
      base.showMsg(res,'删除成功','/admin/admin_list');
      return false;
  }).catch(function(err){
  	 if(err){
  	 	 base.showMsg(res,'删除失败','/amind/amind_list');
  	 	 return false;
  	 }
  });

}